const gulp = require("gulp");
const connect = require("gulp-connect");
const sass = require("gulp-sass");
const concat = require("gulp-concat");
const cleanCss = require("gulp-clean-css");
const sourcemaps = require("gulp-sourcemaps");


//拷贝 html文件
gulp.task("html", done => {
    gulp.src("html/*.html")
        .pipe(gulp.dest("dist/html"))
        .pipe(connect.reload());

    done();
})
// 拷贝 sass文件 转换为css文件
gulp.task("sass", done => {
    gulp.src("sass/*.scss")
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest("dist/css"))
        .pipe(connect.reload());
    done();
})

// 拷贝 js文件到 dist文件
gulp.task("js", done => {
    gulp.src("js/*.js")
        .pipe(gulp.dest("dist/js"))
        .pipe(connect.reload());
    done();
})

// 拷贝图片到 dis文件
gulp.task('img', done => {
    gulp.src('img/*.jpg')
        .pipe(gulp.dest('dist/img'))
        .pipe(connect.reload())
    done();
})


// 监听命令
gulp.task("watch", done => {
    gulp.watch("html/*.html", gulp.series("html"));
    gulp.watch("sass/*.scss", gulp.series("sass"));
    gulp.watch("js/*.js", gulp.series("js"));
    done();
})

// 创建服务器
gulp.task("server", done => {
    connect.server({
        root: "dist",
        livereload: true,
        port: 5000
    })
    done();
})

// chuangjian
gulp.task("build", gulp.parallel("html", "sass", "js", "img"));
gulp.task("default", gulp.series("build", "server", "watch"));